!function (c) {
  "use strict";

  var B,
      s = function (c, B) {
    this.$element = c;
    this.defaults = {
      vSpace: 5,
      server: "",
      captchaIdEle: "",
      captchaValueEle: "",
      imgSize: {
        width: "288px",
        height: "225px"
      },
      blockSize: {
        width: "60px",
        height: "225px"
      },
      barSize: {
        width: "288px",
        height: "60px"
      },
      errCount: 0,
      hideRefreshButton: !1,
      ready: function () {},
      success: function () {},
      error: function () {}
    };
    this.options = $.extend({}, this.defaults, B);
  };

  s.prototype = {
    captchaObj: null,
    init: function () {
      this.options.type == "slip" && (this.captchaObj = new e(this.$element, this.options));
    },
    refresh: function () {
      this.captchaObj.refresh();
    }
  };

  var e = function (c, B) {
    this.$element = $(c);
    this.defaults = {
      vSpace: 5,
      server: "",
      captchaIdEle: "",
      captchaValueEle: "",
      imgSize: {
        width: "288px",
        height: "225px"
      },
      blockSize: {
        width: "60px",
        height: "225px"
      },
      barSize: {
        width: "288px",
        height: "60px"
      },
      hideRefreshButton: !1,
      errCount: 0,
      ready: function () {},
      success: function () {},
      error: function () {}
    };
    this.options = $.extend({}, this.defaults, B);
    this.init();
  };

  e.prototype = {
    captchaid: "",
    init: function () {
      var t = this;
      this.loadDom();
      this.loadImage();
      this.options.ready();

      this.$element[0].onselectstart = document.body.ondrag = function () {
        return !1;
      };

      this.addMoveEvent();
      window.addEventListener("touchmove", function (c) {
        t.move(c);
      });
      window.addEventListener("mousemove", function (c) {
        t.move(c);
      });
      window.addEventListener("touchend", function () {
        t.end();
      });
      window.addEventListener("mouseup", function () {
        t.end();
      });
      t.options.hideRefreshButton && t.$element.find(".verify-refresh").on("click", function () {
        t.refresh();
      });
    },
    addMoveEvent: function () {
      var a = this;
      this.htmlDoms.move_block.on("touchstart", function (c) {
        a.start(c);
      });
      this.htmlDoms.move_block.on("mousedown", function (c) {
        a.start(c);
      });
    },
    loadDom: function () {
      var s = "",
          a = "";
      s += "<div class=\"verify-img-panel\"><img src=\"\" />";
      this.options.hideRefreshButton && (s += "<div  class=\"verify-refresh\"><i class=\"iconfont icon-refresh\"></i></div>");
      s += "</div>";
      a = "<div  class=\"verify-sub-block\"><img src=\"\" /></div>";
      s += "<div class=\"verify-bar-area\"><span  class=\"verify-msg\">向右拖动滑块填充拼图</span><div class=\"verify-left-bar\"><span  class=\"verify-msg\"></span><div  class=\"verify-move-block\"><i  class=\"verify-icon iconfont icon-right\"></i></div>" + a + "</div></div>";
      this.$element.append(s);
      this.htmlDoms = {
        sub_block: this.$element.find(".verify-sub-block"),
        img_panel: this.$element.find(".verify-img-panel"),
        bar_area: this.$element.find(".verify-bar-area"),
        move_block: this.$element.find(".verify-move-block"),
        left_bar: this.$element.find(".verify-left-bar"),
        msg: this.$element.find(".verify-msg"),
        icon: this.$element.find(".verify-icon"),
        refresh: this.$element.find(".verify-refresh")
      };
      this.status = !1;
      this.setSize = this.resetSize(this);
      this.htmlDoms.sub_block.css({
        width: this.options.blockSize.width,
        height: this.options.blockSize.height
      });
      this.htmlDoms.img_panel.css({
        width: this.setSize.img_width,
        height: this.setSize.img_height
      });
      this.htmlDoms.bar_area.css({
        width: this.setSize.bar_width,
        height: this.options.barSize.height,
        "line-height": this.options.barSize.height
      });
      this.htmlDoms.move_block.css({
        width: this.options.barSize.height,
        height: this.options.barSize.height
      });
      this.htmlDoms.left_bar.css({
        width: this.options.barSize.height,
        height: this.options.barSize.height
      });
      this.randSet();
    },
    start: function (c) {
      this.htmlDoms.msg.text("");
      this.htmlDoms.move_block.css("background-color", "#337ab7");
      this.htmlDoms.left_bar.css("border-color", "#337AB7");
      this.htmlDoms.icon.css("color", "#fff");
      c.stopPropagation();
      this.status = !0;
    },
    move: function (c) {
      if (this.status) {
        if (c.touches) t = c.touches[0].pageX;else var t = c.clientX;
        var i = t - e.prototype.getLeft(this.htmlDoms.bar_area[0]);
        i >= this.htmlDoms.bar_area[0].offsetWidth - parseInt(parseInt(this.options.blockSize.width) / 2) - 2 && (i = this.htmlDoms.bar_area[0].offsetWidth - parseInt(parseInt(this.options.blockSize.width) / 2) - 2);
        i <= 0 && (i = parseInt(parseInt(this.options.blockSize.width) / 2));
        var r = i - parseInt(parseInt(this.options.blockSize.width) / 2);
        r < 0 && (r = 0);
        this.htmlDoms.move_block.css("left", r + "px");
        this.htmlDoms.left_bar.css("width", r + "px");
        this.htmlDoms.sub_block.css("left", r + "px");
      }
    },
    end: function () {
      this.status && (this.checkImage(), this.status = !1);
    },
    checkOk: function () {
      this.htmlDoms.move_block.css("background-color", "#fff");
      this.htmlDoms.left_bar.css({
        "border-color": "#5cb85c",
        "background-color": "#5cb85c"
      });
      this.htmlDoms.icon.css("color", "#fff");
      this.htmlDoms.icon.removeClass("icon-right");
      this.htmlDoms.icon.addClass("icon-check");
      this.htmlDoms.refresh.hide();
      this.htmlDoms.move_block.unbind("mousedown touchstart");
      this.options.success();
    },
    checkFail: function (c) {
      var n = this;
      this.htmlDoms.move_block.css("background-color", "#d9534f");
      this.htmlDoms.left_bar.css("border-color", "#d9534f");
      this.htmlDoms.icon.css("color", "#fff");
      this.htmlDoms.icon.removeClass("icon-right");
      this.htmlDoms.icon.addClass("icon-close");
      c == 2100 && (n.$element.find(".verify-left-bar:eq(0)").hide(), n.$element.find(".verify-msg:eq(0)").hide(), n.$element.find(".verify-bar-area:eq(0)").append("<span class=\"verify-error-msg\"  >验证码过期</span>"));
      setTimeout(function () {
        n.refreshPos();
      }, 700);
      this.options.error();
      this.options.errCount++;
      c == 2100 && this.refresh();
      this.options.errCount >= 5 && (this.refresh(), this.options.errCount = 0);
    },
    refreshPos: function () {
      this.$element.find(".verify-left-bar:eq(0)").show();
      this.$element.find(".verify-msg:eq(0)").show();
      this.htmlDoms.move_block.animate({
        left: "0px"
      }, "fast");
      this.htmlDoms.move_block.css("background-color", "#fff");
      this.htmlDoms.sub_block.animate({
        left: "0px"
      }, "fast");
      this.htmlDoms.left_bar.animate({
        width: "60px"
      }, "fast");
      this.htmlDoms.left_bar.css({
        "border-color": "#ddd",
        "background-color": "#f0fff0"
      });
      this.htmlDoms.icon.css("color", "#000");
      this.htmlDoms.icon.removeClass("icon-close");
      this.htmlDoms.icon.addClass("icon-right");
      this.$element.find(".verify-msg:eq(0)").text("向右拖动滑块填充拼图");
      this.$element.find(".verify-error-msg:eq(0)").remove();
      this.addMoveEvent();
    },
    resetSize: function (c) {
      var B,
          s,
          a,
          t,
          e,
          q = c.$element.parent().width() || $(window).width(),
          f = c.$element.parent().height() || $(window).height();
      B = c.options.imgSize.width.indexOf("%") != -1 ? parseInt(c.options.imgSize.width) / 100 * q + "px" : c.options.imgSize.width;
      s = c.options.imgSize.height.indexOf("%") != -1 ? parseInt(c.options.imgSize.height) / 100 * f + "px" : c.options.imgSize.height;
      a = c.options.barSize.width.indexOf("%") != -1 ? parseInt(c.options.barSize.width) / 100 * q + "px" : c.options.barSize.width;
      t = c.options.barSize.height.indexOf("%") != -1 ? parseInt(c.options.barSize.height) / 100 * f + "px" : c.options.barSize.height;
      e = c.options.barSize.width.indexOf("%") != -1 ? parseInt(c.options.blockSize.width) / 100 * f + "px" : c.options.blockSize.width;
      return {
        img_width: B,
        img_height: s,
        bar_width: a,
        bar_height: t,
        block_width: e
      };
    },
    randSet: function () {
      this.$element.find(".verify-img-panel").css("margin-bottom", this.options.vSpace + "px");
      this.$element.find(".verify-sub-block").css({
        top: "-" + (parseInt(this.setSize.img_height) + this.options.vSpace + 2) + "px",
        "background-repeat": "no-repeat",
        "background-size": this.setSize.block_width + " " + this.setSize.img_height,
        "background-position": "center",
        position: "absolute"
      });
    },
    checkImage: function () {
      var s = this.gettimestamp(),
          a = this.gettimestamp(),
          e = this.htmlDoms.move_block.css("left").replace("px", "");
      $("#" + this.options.captchaValueEle).val(e);
      var o = {
        version: "1.0",
        timestamp: s,
        requestid: a,
        request: {
          imagenumber: e,
          captchaid: this.captchaid
        }
      },
          n = this;
      $.ajax({
        url: this.options.server + "check",
        type: "post",
        data: JSON.stringify(o),
        contentType: "application/json",
        success: function (c) {
          c.response.responsecode == 0 ? n.checkOk() : n.checkFail(c.response.responsecode);
          n.status = !1;
        },
        error: function () {
          n.checkFail(responsecode);
          n.status = !1;
        }
      });
    },
    createHttpRequest: function () {
      return window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    },
    loadImage: function () {
      var s = this.gettimestamp(),
          a = {
        version: "1.0",
        timestamp: s,
        request: {
          type: "slip",
          appid: this.options.appid
        }
      },
          t = this,
          e = this.createHttpRequest();
      e.open("post", this.options.server + "get", !0);
      e.responseType = "blob";
      var o;
      e.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      e.onreadystatechange = (o = e, function () {
        if (o.readyState == 4 && o.status == 200) {
          t.captchaid = e.getResponseHeader("gpcaptchaid");
          $("#" + t.options.captchaIdEle).val(t.captchaid);
          var c = e.getResponseHeader("gpcaptchal"),
              B = Number(c.substring(0, 6)),
              s = Number(c.substring(6, 12)),
              a = window.URL || window.webkitURL,
              i = new Blob([this.response]),
              r = t.$element.find(".verify-img-panel >img")[0];
          $(r).load(function (c) {
            a.revokeObjectURL(r.src);
          }).attr("src", a.createObjectURL(i.slice(0, B)));
          var h = t.$element.find(".verify-sub-block >img")[0];
          $(h).load(function (c) {
            a.revokeObjectURL(h.src);
          }).attr("src", a.createObjectURL(i.slice(B, B + s - 1)));
        }
      });
      e.send(JSON.stringify(a));
    },
    refresh: function () {
      this.loadImage();
      this.refreshPos();
    },
    getLeft: function (c) {
      return $(c).offset().left;
    },
    gettimestamp: function () {
      var s = new Date(),
          a = s.getHours(),
          t = s.getMinutes(),
          e = s.getSeconds(),
          i = s.getMilliseconds();
      return this.pad(a, 2) + this.pad(t, 2) + this.pad(e, 2) + this.pad(i, 3);
    },
    pad: function (c, B) {
      for (var t = "", e = c.toString().length; e < B;) {
        t = "0" + t;
        e++;
      }

      return t + c.toString();
    }
  };

  B = function () {
    return this || (0, eval)("this");
  }();

  typeof module !== "undefined" && module.exports ? module.exports = s : typeof define === "function" && define.amd ? define(function () {
    return s;
  }) : !("Captcha" in B) && (B.Captcha = s);
}();